/*
 * TCPSocketSSLSend.cpp
 *
 *  Created on: 2011-3-6
 *      Author: lionxing
 */

#include "TCPSocketSSLSend.h"
#include <charconv.h>
#include <EIKENV.H> 
#include <e32des16.h>	
#include <UTF.H> 

// CONSTANTS
const TInt KTimeout = 10;

CTCPSocketSSLSender* CTCPSocketSSLSender::NewL(MObserverSendSSL& aObserver, CSecureSocket* aSocket)
{
	CTCPSocketSSLSender* self = new(ELeave) CTCPSocketSSLSender(aObserver, aSocket);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop();
	return self;
}

CTCPSocketSSLSender::CTCPSocketSSLSender(MObserverSendSSL& aObserver, CSecureSocket* aSocket) : 
CActive(EPriorityStandard), iObserver(aObserver), iSSLSend(aSocket)	// Standard priority
{
	CActiveScheduler::Add(this);
}

void CTCPSocketSSLSender::ConstructL()
{
	iTimer = CTCPSocketTimer::NewL(*this);
	iBufferPtr=NULL;
}

CTCPSocketSSLSender::~CTCPSocketSSLSender()
{
	delete iTimer;
	delete iBufferPtr;
	Cancel();
}

void CTCPSocketSSLSender::Send(const TDesC8& aMsg)
{
	Cancel();

	TInt size = aMsg.Size();
	delete iBufferPtr;
	iBufferPtr=HBufC8::NewL(size);
	//iBuffer.Copy(pkg);
	iBufferPtr->Des().Copy(aMsg);

	iSSLSend->Send(iBufferPtr->Des(), iStatus);
	SetActive();

	// Start a timeout timer
	iTimer->Start(KTimeout);

	
}

void CTCPSocketSSLSender::DoCancel()
{
	iSSLSend->CancelSend();
}

void CTCPSocketSSLSender::RunL()
{
	iTimer->Cancel();	
	iObserver.OnSendSSL(iBufferPtr->Des(), iStatus.Int());
}

// Functions from CTcpSocketTimer::MTimerObserver
void CTCPSocketSSLSender::OnTimerL(TInt aError)
{	
	TInt err = (aError==KErrNone)?KErrTimedOut:aError;
	iObserver.OnSendSSL(iBufferPtr->Des(), err);
}
